using System;
using System.Collections.Generic;
using System.Text;
using ECommerce.Library.DataStructure;
using Entity;
using ECommerce.Library;

namespace AutoGear.BLL
{
    public class StateTax
    {
        #region StateTax Private Fields

        int _stateTaxId;
        string _stateName = string.Empty;
        string _stateCode = string.Empty;
        double _tax = 0;
        bool _isActive;
        bool _isDeleted;
        DateTime _createDate;

        #endregion

        #region StateTax Constractor
        public StateTax() { }

        public StateTax
        (
            int stateTaxId,
            string stateName,
            string stateCode,
            double tax,
            bool isActive,
            bool isDeleted,
            DateTime createDate
        )
        {
            this._stateTaxId = stateTaxId;
            this._stateName = stateName;
            this._stateCode = stateCode;
            this._tax = tax;
            this._isActive = isActive;
            this._isDeleted = isDeleted;
            this._createDate = createDate;
        }

        public StateTax
        (
          TblStateTax tblStateTax
        )
        {
            this._stateTaxId = tblStateTax.StateTaxId;
            this._stateName = tblStateTax.StateName;
            this._stateCode = tblStateTax.StateCode;
            this._tax = Convert.ToDouble(tblStateTax.Tax);
            this._isActive = tblStateTax.IsActive;
            this._isDeleted = tblStateTax.IsDeleted;
            this._createDate = tblStateTax.CreateDate;
        }
        #endregion

        #region Public Properties
        public int StateTaxId
        {
            get { return _stateTaxId; }
        }
        public string StateName
        {
            get { return _stateName; }
            set { _stateName = value; }
        }
        public string StateCode
        {
            get { return _stateCode; }
            set { _stateCode = value; }
        }
        public double Tax
        {
            get { return _tax; }
            set { _tax = value; }
        }

        public bool IsActive
        {
            get { return this._isActive; }
            set { this._isActive = value; }
        }
        public bool IsDeleted
        {
            get { return this._isDeleted; }
            set { this._isDeleted = value; }
        }
        public DateTime CreateDate
        {
            get { return this._createDate; }
        }

        #endregion

        public void Create()
        {

            try
            {
                this._stateTaxId = TblStateTax.CreateNewTblStateTax(this._stateName, this._stateCode, Convert.ToDecimal(this._tax), this._isActive, this._isDeleted, DateTime.Now);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool Update()
        {
            try
            {
                TblStateTax tblStateTax = TblStateTax.GetTblStateTaxByStateTaxId(this._stateTaxId);
                tblStateTax.StateName = this._stateName;
                tblStateTax.StateCode = this._stateCode;
                tblStateTax.Tax = Convert.ToDecimal(this._tax);
                tblStateTax.IsActive = this._isActive;
                tblStateTax.IsDeleted = this._isDeleted;
                return tblStateTax.Save();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void Delete()
        {
            /**************************************************
             * will update database table, will delete physical
             * files and will write in admin log about the 
             * delete
             **************************************************/
            this._isDeleted = true;
            this.Update();
        }

        public double CalculateTax(double subTotal)
        {
            return (subTotal * this.Tax) / 100.0;
        }

        public static void DeleteStateTax(List<int> idList)
        {
            TblStateTax.DeleteTblStateTaxList(idList);
        }

        public static void DeleteStateTax(ReadWriteCustomCollection<StateTax> StateTax)
        {
            foreach (StateTax stateTax in StateTax)
            {
                stateTax.Delete();
            }
        }

        public static StateTax Load(int stateTaxId)
        {
            try
            {
                TblStateTax tmpTblStateTax = TblStateTax.GetTblStateTaxByStateTaxId(stateTaxId);
                StateTax stateTax = new StateTax(tmpTblStateTax);
                return stateTax;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static ReadWriteCustomCollection<StateTax> GetActiveStateTaxList()
        {
            return LoadFromCustomCollection(TblStateTaxExtended.GetActiveStateTaxPaged("StateName", 0, 99999));
        }


        public static ReadWriteCustomCollection<StateTax> GetBackendStateTaxList(bool includeDeleted, string orderBy, int startIndex, int pageSize)
        {
            if (includeDeleted)
            {
                return LoadFromCustomCollection(TblStateTax.GetAllTblStateTaxPaged(orderBy, startIndex, pageSize));
            }
            else
            {
                return LoadFromCustomCollection(TblStateTaxExtended.GetNonDeletedStateTaxPaged(orderBy, startIndex, pageSize));
            }
        }

        public static int GetBackendStateTaxListCount(bool includeDeleted)
        {
            if (includeDeleted)
            {
                return TblStateTax.GetAllTblStateTaxPagedCount("", 0, 0);
            }
            else
            {
                return TblStateTaxExtended.GetNonDeletedStateTaxCount();
            }
        }

        //public static ReadWriteCustomCollection<StateTax> GetBackendStateTaxListByStateCode(string StateCode, bool includeDeleted, string orderBy, int startIndex, int pageSize)
        //{
        //    if (includeDeleted)
        //    {
        //        return LoadFromCustomCollection(TblStateTaxExtended.GetAllStateTaxByStateCodePaged(StateCode, orderBy, startIndex, pageSize));
        //    }
        //    else
        //    {
        //        return LoadFromCustomCollection(TblStateTaxExtended.GetNonDeletedStateTaxByStateCodePaged(StateCode, orderBy, startIndex, pageSize));
        //    }
        //}

        //public static int GetBackendStateTaxListByStateCodeCount(string StateCode, bool includeDeleted)
        //{
        //    if (includeDeleted)
        //    {
        //        return TblStateTaxExtended.GetAllStateTaxByStateCodeCount(StateCode);
        //    }
        //    else
        //    {
        //        return TblStateTaxExtended.GetNonDeletedStateTaxByStateCodeCount(StateCode);
        //    }
        //}

        public static ReadWriteCustomCollection<StateTax> GetBackendStateTaxListByStateName(string StateName, bool includeDeleted, string orderBy, int startIndex, int pageSize)
        {
            if (includeDeleted)
            {
                return LoadFromCustomCollection(TblStateTaxExtended.GetAllStateTaxByStateNamePaged(StateName, orderBy, startIndex, pageSize));
            }
            else
            {
                return LoadFromCustomCollection(TblStateTaxExtended.GetNonDeletedStateTaxByStateNamePaged(StateName, orderBy, startIndex, pageSize));
            }
        }

        public static int GetBackendStateTaxListByStateNameCount(string StateName, bool includeDeleted)
        {
            if (includeDeleted)
            {
                return TblStateTaxExtended.GetAllStateTaxByStateNameCount(StateName);
            }
            else
            {
                return TblStateTaxExtended.GetNonDeletedStateTaxByStateNameCount(StateName);
            }
        }

        public static StateTax GetStateTaxByStateName(string stateName) {
            ReadWriteCustomCollection<StateTax> stateTaxes = GetBackendStateTaxListByStateName(stateName, false, "StateName", 0, 99999);
            if (stateTaxes.Count>0 ) {
                return stateTaxes[0];
            }
            return new StateTax();
        }
        
        private static ReadWriteCustomCollection<StateTax> LoadFromCustomCollection(ECommerce.DAL.CustomCollection<TblStateTax> tblStateTax)
        {
            ReadWriteCustomCollection<StateTax> result = new ReadWriteCustomCollection<StateTax>();
            for (int i = 0; i < tblStateTax.Count; i++)
            {
                TblStateTax tmpTblStateTax = tblStateTax[i];
                result.Add(new StateTax(tmpTblStateTax));
            }
            return result;
        }

        public static StateTax GetStateTaxByStateCode(string stateCode)
        {
            stateCode = Security.BindStringInput(stateCode, SecurityLevel.Zero);
            TblStateTax stateTaxeData = TblStateTaxExtended.GetStateTaxByStateCode(stateCode);
            if (stateTaxeData != null)
                return new StateTax(stateTaxeData);
            else
                return null;
        }
    }
}
